<template>
  <div class="dashboard-container">
    <el-card>

      <!-- //^顶部搜索 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <!--clearable 是否可清空-->
          <el-input v-model="userInfo.query" placeholder="请输入内容" clearable @clear="getGoodsList" @keyup.enter.native="getGoodsList">
            <template #append>
              <el-button icon="el-icon-search" @click="getGoodsList" />
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="$router.push('/goods/add')">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- //^商品列表区-->
      <el-table :data="goodslist" border stripe>
        <el-table-column type="index" />
        <el-table-column prop="goods_name" label="商品名称" />
        <el-table-column label="商品价格(元)" prop="goods_price" width="95px" />
        <el-table-column label="商品重量" prop="goods_weight" width="70px" />
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" />
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeById(scope.row.goods_id)" />
          </template>
        </el-table-column>
      </el-table>

      <!-- //^分页区域 -->
      <!--:current-page 当前显示第几页的数据-->
      <!--:page-size 当前每页显示多少条数据-->
      <!--layout  要展示的功能组件-->
      <!--:total 总数据条数-->
      <el-pagination :current-page="userInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="userInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    </el-card>
  </div>
</template>

<script>
import { getGoodsList } from '@/api/goods'

export default {
  data() {
    return {
      // 查询参数对象
      userInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goodslist: [],
      // 总数据条数
      total: 0
    }
  },

  created() {
    this.getGoodsList()
  },

  methods: {
    // ^获取商品列表
    async getGoodsList() {
      const res = await getGoodsList(this.userInfo)
      // console.log(res)
      this.goodslist = res.goodslist
      this.total = res.total
    },

    // ^分页区域操作
    // 监听pagesize改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.userInfo.pagesize = newSize
      this.getUserList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.userInfo.pagenum = newPage
      this.getUserList()
    },

    // ^添加商品
    addGoods() {}
  }
}
</script>

<style scoped lang='less'>
.el-table {
  margin-top: 15px;
  font-size: 12px;
}
.el-pagination {
  margin-top: 15px;
}
</style>
